import { searchCover, deepCopy, multipleChecked, getField } from "@/common.js"
// import { post } from "@/utils/network.js"
// 多选
export const checkedItem = function (optItem) {
  let options = multipleChecked(deepCopy(this.item.options), {id: optItem.id})
  const value = getField(options, 'id', {isChecked: true}).join(',')
  // 更新当前条目的值
  const item = {...this.item, options, value}
  if(value.length) {
    item.errmsg = ''
  }
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => {
    v.options = options
    v.value = value
    if(value.length) { v.errmsg = '' }
    return v
  })
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => {
    v.options = options
    v.value = value
    if(value.length) { v.errmsg = '' }
    return v
  }) : v.children}))
  this.$emit('setFormData', formData)
  eval(this.item.onChange)(options.filter(v => v.isChecked))
}
// 获取单选的选项列表
export const getOptions = async function () {
  // if(!this.item.optionsUrl) { return false }
  // const options = await post(this.item.optionsUrl, {})
  // let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, options: options}))
  // formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, options: options})) : v.children}))
  // this.$emit('setFormData', formData)
}